import java.util.HashMap;

public class Solution290 {
    public boolean wordPattern(String pattern, String s) {
        String[] strings = s.split(" ");
        if (pattern.length() != strings.length) {
            return false;
        }
        HashMap<Character, String> map1 = new HashMap<>();
        HashMap<String, Character> map2 = new HashMap<>();
        for (int i = 0; i < pattern.length(); i++) {
            Character c = pattern.charAt(i);
            if (map1.containsKey(c) || map2.containsKey(strings[i])) {
                if (!map1.containsKey(c) || !map1.get(c).equals(strings[i])) {
                    return false;
                }
                if (!map2.containsKey(strings[i]) || !map2.get(strings[i]).equals(c)) {
                    return false;
                }
            } else {
                map1.put(c, strings[i]);
                map2.put(strings[i], c);
            }
        }
        return true;
    }
}
